<?php

/**
 * Created by PhpStorm.
 * User: songshuai
 * Date: 2019/7/16
 * Time: 15:52
 */

namespace App\Jobs;

use http\Exception\BadMessageException;
use Illuminate\Support\Facades\DB;
use App\admin\Service\OA;
class Fun {

    const ERROR_SHOW=1;
    public static function date($time = 0,$showtime=true) {
        if ($time === 0)
            return '';
        if($showtime){
             return date("Y-m-d H:i:s", $time);
        }else{
             return date("Y-m-d", $time);
        }
       
    }
    public static function encode($content, $doubleEncode = true)
    {
        return htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
    }

    /**
     * 校验日期格式是否正确
     *
     * @param string $date 日期
     * @param string $formats 需要检验的格式数组
     * @return boolean
     */
  public static  function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d")) {
        $unixTime = strtotime($date);
        if (!$unixTime) { //strtotime转换不对，日期格式显然不对。
            return false;
        }
        //校验日期的有效性，只要满足其中一个格式就OK
        foreach ($formats as $format) {
            if (date($format, $unixTime) == $date) {
                return true;
            }
        }

        return false;
    }


    public static function showmsg($msg,$type=self::ERROR_SHOW){
      if(self::isAjax()){
          OA::Json(1,$msg);
      }
        echo "<h1>".$msg."</h1>";
      //  throw new BadMessageException($msg);
        exit;
    }


    public static  function isAjax()
    {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
    }

    /**@var
     * 网站域名
     */
    public static $HOST='http://www.fuzhuangchangongxiao.cn';

    /**
     * 网站默认图片
    */
    public static $Pic='/h5/images/logo-di.png';


    /**
     * 转换图片链接
    */
    public static function pImg($src){
        if(empty($src)){
            return self::$Pic;
        }
        if($src[0]==='/'){
            $src=self::$HOST.$src;
        }
        return $src;
    }






    /**
     * 获取ip地址
     */
    public static function Ip() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
            $ip = getenv("REMOTE_ADDR");
        else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
            $ip = $_SERVER['REMOTE_ADDR'];
        else
            $ip = "";

        if (empty($ip))
            return Yii::$app->request->userIP;

        $albumlist = explode(",", $ip);
        return $albumlist[0];
    }



    /**
     * 判断是否为手机版
     */
    public static function UA() {
        if (!isset($_SERVER['HTTP_USER_AGENT']))
        {
            return '';
        }else{
            return $_SERVER['HTTP_USER_AGENT'];
        }
    }
    
    
    /**
     * 跳转到登录
    */
    public static function goLogin(){
        if(self::isAjax()){
            OA::Json(OA::Guest);
            return null;
        }else{
          return   redirect('/h5/login/login.html');  
        }
    }



    /**
     * Curl版本
     * 使用方法：
     * $post_string = "app=request&version=beta";
     * request_by_curl('http://facebook.cn/restServer.php',$post_string);
     */
    public static function request_by_curl($remote_server, $post_string) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_string));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "");
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }




}
